<?php
require 'BaseController.php';
class Admin_SupporterController extends Admin_BaseController{
	private $_supporterModel;
	
	public function init(){
		parent::init();
		$auth = Zend_Auth::getInstance();
		$this->_identity = $auth->getIdentity();
		$this->view->identity = $this->_identity;
		$this->_supporterModel = new Model_Supporter();
	}
	
	public function listAction(){
		$session = new Zend_Session_Namespace();
		$this->view->message = $session->message;
		$session->__unset('message');
		$auth = Zend_Auth::getInstance();
		$this->view->arrSupporters= $this->_supporterModel->getAllSupporters();
		$this->view->identity= $auth->getIdentity();
	}

	public function createAction(){
		$session = new Zend_Session_Namespace();
		$auth = Zend_Auth::getInstance();
		$this->view->identity = $auth->getIdentity();
		$createForm = new Form_CreateSupporter();
		$this->view->form =  $createForm;
		if($this->_request->isPost() && $createForm->isValid($_POST)){
				//lấy dữ liệu
				$name = $createForm->getValue("name");				
				$email = $createForm->getValue("email");
				$phone = $createForm->getValue("phone");
				$yahoo = $createForm->getValue("yahoo");
				$type = $createForm->getValue("type");
				$id = $this->_supporterModel->createSupporter($name,$email, $phone, $yahoo, $type);
				
				if(isset($id)){
					$session->message = "<span class='success'>Thêm supporter thành công</span>";
					$this->_redirector->gotoRoute(array(),'admin_supporter_list');
				}else{
					$session->message = "<span class='error'>Thêm supporter không thành công</span>";
				}
		}
	}
	
	public function editAction(){
	//khởi tạo session
		$session = new Zend_Session_Namespace();
		//truyền identity cho view
		$auth = Zend_Auth::getInstance();
		$this->view->identity = $auth->getIdentity();
		//tạo form
		$createForm = new Form_CreateSupporter();
		$this->view->form =  $createForm;
		//lấy user theo id
		$id = $this->getRequest()->getParam('id');
		if(isset($id)){
			$supporter = $this->_supporterModel->getSupporterByID($id);
			if($supporter == null){
				$this->view->message = "<span class='error'>Không tồn tại supporter này.</span>"; 
			}else{
				$data = array(
								'name' => $supporter->name,
								'email' => $supporter->email,
								'phone' => $supporter->phone,
								'yahoo' => $supporter->yahoo_account,
								'type' => $supporter->type
							);
				$createForm->populate($data);
			}
			//kiểm tra post
			if($this->_request->isPost() && $createForm->isValid($_POST)){
				//lấy dữ liệu
				$name = $createForm->getValue("name");
				$email = $createForm->getValue("email");
				$phone = $createForm->getValue("phone");
				$yahoo = $createForm->getValue("yahoo");
				$type = $createForm->getValue("type");
				$result = $this->_supporterModel->updateSupporter($id, $name, $phone, $email, $yahoo, $type);
				if($result == 1){
					$session->message = "<span class='success'>Sửa thông tin supporter thành công</span>";
					$this->_redirector->gotoRoute(array(),'admin_supporter_list');
				}else{
					$session->message = "<span class='error'>Sửa thông tin supporter không thành công</span>";
				}
				
			}
		}else{
			$this->view->message = "<span class='error'>Có lỗi xảy ra.</span>"; 
		}
	}
	public function deleteAction(){
		$session = new Zend_Session_Namespace();
		//truyền identity cho view
		$auth = Zend_Auth::getInstance();
		$this->view->identity = $auth->getIdentity();
		//lấy id cần xóa
		if($this->getRequest()->isPost()){
			$id = $this->getRequest()->getParam('ids');
		}else{
			$id = $this->getRequest()->getParam('id');
		}
		if($id){
			if($this->_supporterModel->deleteSupporter($id) > 0){
				$session->message = "<span class='success'>Xóa supporter thành công</span>";
				
			}else{
				$session->message = "<span class='error'>Xóa supporter không thành công</span>";
				
			}
		}else{
			$session->message = "<span class='error'>Có lỗi xảy ra</span>";
		}
		$this->_redirector->gotoRoute(array(),'admin_supporter_list');
	}
}